package com.infore.statisticsAndAlarm.service.impl;

import static org.hamcrest.CoreMatchers.nullValue;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.statisticsAndAlarm.constant.Constants;
import com.infore.statisticsAndAlarm.constant.ResultConstants;
import com.infore.statisticsAndAlarm.dto.OperationRealDataDto;
import com.infore.statisticsAndAlarm.dto.WaterInstructionsDataDto;
import com.infore.statisticsAndAlarm.enums.ResultStatus;
import com.infore.statisticsAndAlarm.externalInterface.IDataReceivingService;
import com.infore.statisticsAndAlarm.externalInterface.IStieMangementService;
import com.infore.statisticsAndAlarm.outsideEntity.Project;
import com.infore.statisticsAndAlarm.service.IOperationDataService;
import com.infore.statisticsAndAlarm.util.ArrayUtils;
import com.infore.statisticsAndAlarm.util.CookiesUtil;
import com.infore.statisticsAndAlarm.util.DateUtil;
import com.infore.statisticsAndAlarm.util.MyBeanUtils;
import com.infore.statisticsAndAlarm.util.ObjectMapperUtil;
import com.infore.statisticsAndAlarm.util.Page;
import com.infore.statisticsAndAlarm.util.ReflexUtil;
import com.infore.statisticsAndAlarm.util.ResultEntity;
import com.infore.statisticsAndAlarm.util.ScaleUtil;
import com.infore.statisticsAndAlarm.util.WaterLevelUtils;
import com.infore.statisticsAndAlarm.util.WaterQualityUtil;
import com.infore.statisticsAndAlarm.vo.OperationRealDataVo;
import com.infore.statisticsAndAlarm.vo.ProjectRealDataVo;

@Service
public class IOperationDataServiceImpl implements IOperationDataService {

	@Autowired
	private IStieMangementService iStieMangementService;
	@Autowired
	private IDataReceivingService iDataReceivingService;
	@Autowired
	private ObjectMapperUtil objectMapperUtil;
	@Autowired
	private WaterLevelUtils WaterLevelUtils;
	private final String water_station_type = "2c9212e66f323a55016f59867a200030";
	private final String air_station_type = "2c9212e66f0241df016f1659ce77001f";

	@SuppressWarnings("unchecked")
	List<OperationRealDataVo> getRealDataResult(Map<String, Project> projectMap, List<Map<String, Object>> datas,
			List<Map<String, Object>> stations, Map<String, List<Map<String, Object>>> stationAndProjectMap) {
		Map<String, List<Map<String, Object>>> gDatas = datas.stream()// 小时数据根据站点分组
				.collect(Collectors.groupingBy(g -> g.get("MN").toString()));
		List<OperationRealDataVo> list = new ArrayList<OperationRealDataVo>();
		// 遍历存储数据
		for (Map<String, Object> station : stations) {
			OperationRealDataVo operationRealDataVo = new OperationRealDataVo();
			List<ProjectRealDataVo> projectRealDataVos = new ArrayList<ProjectRealDataVo>();
			String state = "在线";
			String mn = station.get("STATION_MN").toString();
			String address = station.get("STATION_ADDRESS") == null ? "" : station.get("STATION_ADDRESS").toString();
			String name = station.get("STATION_NAME").toString();
			List<Map<String, Object>> data = gDatas.get(mn);// 得到站点下小时数据
			Map<String, List<Map<String, Object>>> gData = new HashMap<String, List<Map<String, Object>>>();
			if (data == null || data.size() == 0) {
				state = "离线";
			} else {
				gData = data.stream().filter(f -> f.get("CODE") != null)// 对站点下的小时数据按照因子进行分组
						.collect(Collectors.groupingBy(g -> g.get("CODE").toString()));
			}
			List<Map<String, Object>> staAndPros = stationAndProjectMap.get(mn);// 站点下因子中间表
			if (staAndPros != null && staAndPros.size() != 0) {
				for (Map<String, Object> staAndPro : staAndPros) {
					ProjectRealDataVo projectRealDataVo = new ProjectRealDataVo();
					String code = staAndPro.get("PROJECT_CODE").toString();
					Project project = MyBeanUtils.convertMapToBean(// 得到因子具体信息
							(Map<String, Object>) projectMap.get(code), Project.class);
					List<Map<String, Object>> singleData = gData.get(code);
					if (singleData != null && singleData.size() > 0) {
						Map<String, Object> map = singleData.get(0);// 得到第一条数据，查询时已经按照时间倒序
						String value = map.get("VALUE").toString();
						String time = map.get("TIME").toString();
						projectRealDataVo.setValue(value);
						projectRealDataVo.setTime(time);
					}
					projectRealDataVo.setCode(code);
					projectRealDataVo.setName(project.getProjectName());
					projectRealDataVo.setUnit(project.getProjectUnit());
					projectRealDataVos.add(projectRealDataVo);
				}
			}
			operationRealDataVo.setMn(mn);
			operationRealDataVo.setName(name);
			operationRealDataVo.setState(state);
			operationRealDataVo.setAddress(address);
			operationRealDataVo.setProjectRealDataVos(projectRealDataVos);
			list.add(operationRealDataVo);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public ResultEntity queryRealTime(String stationType, String key, String stationMns, Integer pageNo,
			Integer pageSize, HttpServletRequest request) {
		// 查询站点数据
		OperationRealDataDto operationRealDto = new OperationRealDataDto();
		String platfrom = CookiesUtil.getCookieValue(request, "platformId");
		String appCode = CookiesUtil.getCookieValue(request, "appCode");
		String userId = CookiesUtil.getCookieValue(request, "userId");
		String admin = CookiesUtil.getCookieValue(request, "admin");
		String roleId = CookiesUtil.getCookieValue(request, "roleId");
		operationRealDto.setAdmin(admin);
		operationRealDto.setAppCode(appCode);
		operationRealDto.setKey(key);
		operationRealDto.setPageNo(pageNo);
		operationRealDto.setPageSize(pageSize);
		operationRealDto.setPlatform(platfrom);
		operationRealDto.setStationMn(stationMns);
		operationRealDto.setStationType(stationType);
		operationRealDto.setUserId(userId);
		operationRealDto.setRoleId(roleId);
		// 得到站点和监测因子中间表
		ResultEntity stationInfo = iStieMangementService.operationRealData(operationRealDto);
		List<Map<String, Object>> stations = (List<Map<String, Object>>) ((Map<String, Object>) stationInfo
				.getContent()).get("dataList");
		if (stations == null || stations.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS);
		}
		List<Map<String, Object>> stationAndProject = (List<Map<String, Object>>) ((Map<String, Object>) stationInfo
				.getContent()).get("info");
		Integer totalPage = (Integer) ((Map<String, Object>) stationInfo.getContent()).get("pageTotle");
		Integer totalRecord = (Integer) ((Map<String, Object>) stationInfo.getContent()).get("totalRecord");
		Map<String, List<Map<String, Object>>> stationAndProjectMap = stationAndProject.stream()
				.collect(Collectors.groupingBy(g -> g.get("STATION_CODE").toString()));
		List<String> mns = stations.stream().filter(f -> f.get("STATION_MN").toString() != null)
				.map(m -> m.get("STATION_MN").toString()).collect(Collectors.toList());
		// 查询站点下因子数据
		// 实时数据--时间暂时默认查八小时之内
		SimpleDateFormat format = new SimpleDateFormat(DateUtil.DATE_FULL_STR);
		String endTime = format.format(new Date());
		String startTime = DateUtil.addHour(endTime, -6, DateUtil.DATE_FULL_STR);
		// 判断是查空气还是水
		List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
		if (mns != null && mns.size() != 0) {
			if (water_station_type.equals(stationType)) {
				ResultEntity resDataWater = iDataReceivingService.operationRealData(StringUtils.join(mns, ","),
						startTime, endTime);
				List<Map<String, Object>> datasWater = objectMapperUtil.convertListMap(resDataWater);
				datas.addAll(datasWater);
			} else if (air_station_type.equals(stationType)) {
				String startTimeAir = DateUtil.addMin(new Date(), -30, DateUtil.DATE_FULL_STR);// 空气默认半小时内的数据
				ResultEntity resDataAir = iDataReceivingService.operationRealDataAir(StringUtils.join(mns, ","),
						startTimeAir, endTime);
				List<Map<String, Object>> datasAir = objectMapperUtil.convertListMap(resDataAir);
				datas.addAll(datasAir);
			} else if (StringUtils.isBlank(stationType)) {
				ResultEntity resDataWater = iDataReceivingService.operationRealData(StringUtils.join(mns, ","),
						startTime, endTime);
				List<Map<String, Object>> datasWater = objectMapperUtil.convertListMap(resDataWater);
				datas.addAll(datasWater);
				String startTimeAir = DateUtil.addMin(new Date(), -30, DateUtil.DATE_FULL_STR);// 空气默认半小时内的数据
				ResultEntity resDataAir = iDataReceivingService.operationRealDataAir(StringUtils.join(mns, ","),
						startTimeAir, endTime);
				List<Map<String, Object>> datasAir = objectMapperUtil.convertListMap(resDataAir);
				datas.addAll(datasAir);
			}
		}
		// 得到所有因子信息
		// 获得因子信息
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByProjectCode();
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// 封装数据
		List<OperationRealDataVo> list = getRealDataResult(projectMap, datas, stations, stationAndProjectMap);
		Page<OperationRealDataVo> page = new Page<OperationRealDataVo>();
		page.setList(list);
		page.setPageSize(pageSize);
		page.setPageNo(pageNo);
		page.setTotalRecords(totalRecord);
		page.setTotalPages(totalPage);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
	}

	/*****************************************************************/

	@SuppressWarnings("unchecked")
	List<OperationRealDataVo> getRealDataGroupResult(Map<String, Project> projectMap, List<Map<String, Object>> datas,
			List<Map<String, Object>> stations) {
		Map<String, List<Map<String, Object>>> gDatas = datas.stream()// 小时数据根据站点分组
				.collect(Collectors.groupingBy(g -> g.get("MN").toString()));
		List<OperationRealDataVo> list = new ArrayList<OperationRealDataVo>();
		// 遍历存储数据
		for (Map<String, Object> station : stations) {
			OperationRealDataVo operationRealDataVo = new OperationRealDataVo();
			List<ProjectRealDataVo> projectRealDataVos = new ArrayList<ProjectRealDataVo>();
			String mn = station.get("STATION_MN").toString();
			String address = station.get("STATION_ADDRESS") == null ? "" : station.get("STATION_ADDRESS").toString();
			String province = station.get("PROVINCE_NAME") == null ? "" : station.get("PROVINCE_NAME").toString();
			String name = station.get("STATION_NAME").toString();
			String basinn_name = station.get("BASINN_NAME") == null ? "" : station.get("BASINN_NAME").toString();
			List<Map<String, Object>> data = gDatas.get(mn);// 得到站点下小时数据
			if (data != null && data.size() > 0) {
				String dataTime = data.get(0).get("TIME") == null ? "" : data.get(0).get("TIME").toString();// 数据时间
				List<WaterInstructionsDataDto> finalList = new ArrayList<>();// 计算水质类别工具
				WaterInstructionsDataDto waterInstructionsDataDto = new WaterInstructionsDataDto();
				finalList.add(waterInstructionsDataDto);
				if (data != null && data.size() > 0) {
					Map<String, List<Map<String, Object>>> gData = data.stream().filter(f -> f.get("CODE") != null)// 对站点下的小时数据按照因子进行分组
							.collect(Collectors.groupingBy(g -> g.get("CODE").toString()));
					Set<String> staAndPros = gData.keySet();// 站点下因子中间表
					for (String staAndPro : staAndPros) {
						ProjectRealDataVo projectRealDataVo = new ProjectRealDataVo();
						String code = staAndPro;
						Project project = MyBeanUtils.convertMapToBean(// 得到因子具体信息
								(Map<String, Object>) projectMap.get(code), Project.class);
						if (project == null) {
							continue;
						}
						List<Map<String, Object>> singleData = gData.get(code);
						if (singleData != null && singleData.size() > 0) {
							Map<String, Object> map = singleData.get(0);// 得到第一条数据，查询时已经按照时间倒序
							String value = map.get("VALUE").toString();
							String time = map.get("TIME").toString();
							projectRealDataVo.setValue(value);
							projectRealDataVo.setTime(time);
							ReflexUtil.setValue(waterInstructionsDataDto, WaterInstructionsDataDto.class, code,
									String.class, value);
						}
						projectRealDataVo.setCode(code);
						projectRealDataVo.setName(project.getProjectName() == null ? "" : project.getProjectName());
						projectRealDataVo.setUnit(project.getProjectUnit());
						projectRealDataVos.add(projectRealDataVo);
					}
				}
				WaterQualityUtil.setWaterQuality(finalList);
				operationRealDataVo.setDataTime(dataTime);
				operationRealDataVo.setLevel(
						WaterLevelUtils.exchangeWaterLevelToChinese(waterInstructionsDataDto.getWaterQuality()));
			}
			operationRealDataVo.setMn(mn);
			operationRealDataVo.setName(name);
			operationRealDataVo.setAddress(address);
			operationRealDataVo.setProvince(province);
			operationRealDataVo.setBasinnName(basinn_name);
			operationRealDataVo.setProjectRealDataVos(projectRealDataVos);
			list.add(operationRealDataVo);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public ResultEntity queryRealTimeGroup(String stationType, String key, String stationMns, Integer pageNo,
			Integer pageSize, HttpServletRequest request) {
		// 查询站点数据
		OperationRealDataDto operationRealDto = new OperationRealDataDto();
		String platfrom = CookiesUtil.getCookieValue(request, "platformId");
		String appCode = CookiesUtil.getCookieValue(request, "appCode");
		String userId = CookiesUtil.getCookieValue(request, "userId");
		String admin = CookiesUtil.getCookieValue(request, "admin");
		String roleId = CookiesUtil.getCookieValue(request, "roleId");
		operationRealDto.setAdmin(admin);
		operationRealDto.setAppCode(appCode);
		operationRealDto.setKey(key);
		operationRealDto.setPageNo(pageNo);
		operationRealDto.setPageSize(pageSize);
		operationRealDto.setPlatform(platfrom);
		operationRealDto.setStationMn(stationMns);
		operationRealDto.setStationType(stationType);
		operationRealDto.setUserId(userId);
		operationRealDto.setRoleId(roleId);
		// 得到站点和监测因子中间表
		ResultEntity stationInfo = iStieMangementService.operationRealDataType(operationRealDto);
		List<Map<String, Object>> stations = (List<Map<String, Object>>) ((Map<String, Object>) stationInfo
				.getContent()).get("dataList");
		Integer totalPage = (Integer) ((Map<String, Object>) stationInfo.getContent()).get("pageTotle");
		Integer totalRecord = (Integer) ((Map<String, Object>) stationInfo.getContent()).get("totalRecord");
		List<String> mns = stations.stream().filter(f -> f.get("STATION_MN").toString() != null)
				.map(m -> m.get("STATION_MN").toString()).collect(Collectors.toList());
		// 查询站点下因子数据
		// 实时数据--时间暂时默认查八小时之内
		SimpleDateFormat format = new SimpleDateFormat(DateUtil.DATE_FULL_STR);
		String endTime = format.format(new Date());
		String startTime = DateUtil.addHour(endTime, -6, DateUtil.DATE_FULL_STR);
		// 判断是查空气还是水
		List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
		if (mns != null && mns.size() != 0) {
			if (water_station_type.equals(stationType)) {
				ResultEntity resDataWater = iDataReceivingService.operationRealData(StringUtils.join(mns, ","),
						startTime, endTime);
				List<Map<String, Object>> datasWater = objectMapperUtil.convertListMap(resDataWater);
				datas.addAll(datasWater);
			} else if (air_station_type.equals(stationType)) {
				String startTimeAir = DateUtil.addMin(new Date(), -30, DateUtil.DATE_FULL_STR);// 空气默认半小时内的数据
				ResultEntity resDataAir = iDataReceivingService.operationRealDataAir(StringUtils.join(mns, ","),
						startTimeAir, endTime);
				List<Map<String, Object>> datasAir = objectMapperUtil.convertListMap(resDataAir);
				datas.addAll(datasAir);
			} else if (StringUtils.isBlank(stationType)) {
				ResultEntity resDataWater = iDataReceivingService.operationRealData(StringUtils.join(mns, ","),
						startTime, endTime);
				List<Map<String, Object>> datasWater = objectMapperUtil.convertListMap(resDataWater);
				datas.addAll(datasWater);
				String startTimeAir = DateUtil.addMin(new Date(), -30, DateUtil.DATE_FULL_STR);// 空气默认半小时内的数据
				ResultEntity resDataAir = iDataReceivingService.operationRealDataAir(StringUtils.join(mns, ","),
						startTimeAir, endTime);
				List<Map<String, Object>> datasAir = objectMapperUtil.convertListMap(resDataAir);
				datas.addAll(datasAir);
			}
		}
		// 得到所有因子信息
		// 获得因子信息
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByProjectCode();
		Map<String, Project> projectMap = null;
		List<Map<String, Object>> projectTotaList = new ArrayList<Map<String, Object>>();
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
			if (projectMap != null) {
				for (Entry<String, Project> projectCode : projectMap.entrySet()) {
					Project project = MyBeanUtils.convertMapToBean(
							(Map<String, Object>) projectMap.get(projectCode.getKey()), Project.class);
					if (project.getProjectType() != null
							&& stationType.equals(project.getProjectType().getProjectTypeId())) {
						Map<String, Object> projectMapList = new HashMap<String, Object>();
						projectMapList.put("unit", project.getProjectUnit());
						projectMapList.put("code", project.getProjectCode());
						projectMapList.put("name", project.getProjectName());
						projectTotaList.add(projectMapList);
					}
				}
			}
		}
		// 封装数据
		List<OperationRealDataVo> list = getRealDataGroupResult(projectMap, datas, stations);
		Page<OperationRealDataVo> page = new Page<OperationRealDataVo>();
		page.setList(list);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setTotalPages(totalPage);
		page.setTotalRecords(totalRecord);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page).setInfo(projectTotaList);
	}

	/********************************************************/

	@SuppressWarnings("unchecked")
	public ResultEntity queryHistory(String stationType, String stationMns, Integer pageNo, Integer pageSize,
			HttpServletRequest request, String start, String end, String projectCodeList) {
		List<String> mnList = ArrayUtils.stringToList(stationMns);
		// 得到所有因子编码
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByMn(mnList);
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		List<String> projectCodes = new ArrayList<>(projectMap.keySet());
		// 获得因子信息
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		List<Map<String, Object>> projectTotaList = new ArrayList<Map<String, Object>>();
		if (projectMap != null) {
			for (Entry<String, Project> projectCode : projectMap.entrySet()) {
				Project project = MyBeanUtils
						.convertMapToBean((Map<String, Object>) projectMap.get(projectCode.getKey()), Project.class);
				if (stationType.equals(project.getProjectType().getProjectTypeId())) {
					Map<String, Object> projectMapList = new HashMap<String, Object>();
					projectMapList.put("unit", project.getProjectUnit());// 因子单位
					projectMapList.put("code", project.getProjectCode());// 因子编码
					projectMapList.put("name", project.getProjectName());// 因子名称
					if (StringUtils.isBlank(projectCodeList)) {
						projectTotaList.add(projectMapList);
					}
					if (StringUtils.isNotBlank(projectCodeList) && projectCodeList.contains(project.getProjectCode())) {
						projectTotaList.add(projectMapList);
					}
				}
			}
		}
		// 站点名称
		ResultEntity stationNameResultEntity = iStieMangementService.queryNameByStationMn(stationMns);
		List<Map<String, Object>> stationNameList = objectMapperUtil.convertListMap(stationNameResultEntity);
		Map<String, Object> stationNameMap = new HashMap<String, Object>();
		for (Map<String, Object> map : stationNameList) {
			stationNameMap.put(map.get("MN").toString(), map);
		}
		String pattern = "";
		List<Map<String, Object>> dataResult = new ArrayList<Map<String, Object>>();// 小时天月数据结果集
		// 判断站点编码和因子因子编码是否存在
		if (StringUtils.isBlank(stationMns) || projectCodes == null || projectCodes.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(null);
		}
		// 按时间分发给不同的接口查询，并设置起止时间
		pattern = DateUtil.DATE_SMALL_YMDHH;// 时间转换格式，天要查小时表
		String codeString = StringUtils.join(projectCodes, ",");
		// 得到空气站和水站数据
		ResultEntity resultEntity = iDataReceivingService.audioSingReport(stationMns, codeString, start, end);
		if (resultEntity.getCode() != 0) {
			return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.NO_PROJECT);
		}
		ResultEntity airRes = iDataReceivingService.operationRealDataAir(stationMns, start, end);
		if (airRes.getCode() != 0) {
			return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.NO_PROJECT);
		}
		List<Map<String, Object>> dataResultWater = objectMapperUtil.convertListMap(resultEntity);
		List<Map<String, Object>> dataResultAir = objectMapperUtil.convertListMap(resultEntity);
		if (dataResultAir != null && dataResultAir.size() > 0) {
			dataResult.addAll(dataResultAir);
		}
		if (dataResultWater != null && dataResultWater.size() > 0) {
			dataResult.addAll(dataResultWater);
		}
		return audioMoreReportsAll(projectResultEntity, projectTotaList, stationNameMap, projectCodes, dataResult,
				start, end, mnList, pattern);
	}

	@SuppressWarnings({ "unchecked", "unused" })
	public ResultEntity audioMoreReportsAll(ResultEntity projectResultEntity, List<Map<String, Object>> projectTotaList,
			Map<String, Object> stationNameMap, List<String> projectCodes, List<Map<String, Object>> dataResult,
			String startTime, String endTime, List<String> mnList, String pattern) {
		// 查询因子值数据集
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();// 结果集
		if (dataResult == null || dataResult.size() == 0) {
			Map<String, Object> messageMap = new HashMap<String, Object>();
			messageMap.put("message", "");
			messageMap.put("project", projectTotaList);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list)
					.setInfo(messageMap);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();// 存储每一条数据
		Map<String, Object> valueMap = new HashMap<String, Object>();// 水质等级计算，存储记录类
		List<WaterInstructionsDataDto> InstructionsList = new ArrayList<WaterInstructionsDataDto>();
		// 获得因子信息
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// 遍历因子值数据集，设置相关的值
		for (Map<String, Object> dataResultMap : dataResult) {
			String mn = dataResultMap.get("MN").toString();
			String projectCode = dataResultMap.get("CODE").toString();
			String value = dataResultMap.get("VALUE").toString();
			String timeSingle = dataResultMap.get("TIME").toString();
			String projectName = "";
			String decimalDigits = "3";
			if (projectMap != null) {
				Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(projectCode),
						Project.class);
				projectName = project.getProjectName();
				decimalDigits = project.getDecimalDigits();
			}
			value = ScaleUtil.sciCalProjectString(value, decimalDigits);// 数据修约
			// 如果当前站点下没有因子相关信息，则创建新的，否则在原有的上追加
			if (!valueMap.containsKey(mn + timeSingle)) {
				resultMap = new HashMap<String, Object>();
				WaterInstructionsDataDto Instructions = new WaterInstructionsDataDto();// 计算水质等级封装类
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				InstructionsList.add(Instructions);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterQuery = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put("mn", mn);// 站点mn
				Map<String, Object> mnInfo = (Map<String, Object>) stationNameMap.get(mn);
				String name = mnInfo.get("NAME").toString();
				String province = mnInfo.get("PROVINCE_NAME") == null ? "" : mnInfo.get("PROVINCE_NAME").toString();
				String basinn = mnInfo.get("BASINN_NAME") == null ? "" : mnInfo.get("BASINN_NAME").toString();
				resultMap.put("name", name);// 站点名称
				resultMap.put("province", province);//
				resultMap.put("basinn", basinn);//
				resultMap.put("time", timeSingle);//
				resultMap.put(projectCode, value);// 因子编号和因子值
				resultMap.put("level", waterQuery);// 实际水质等级
				list.add(resultMap);
				valueMap.put(mn + timeSingle, Instructions);
			} else {
				WaterInstructionsDataDto Instructions = (WaterInstructionsDataDto) valueMap.get(mn + timeSingle);
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterQuery = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put(projectCode, value);
				resultMap.put("level", waterQuery);
			}
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list)
				.setInfo(projectTotaList);
	}
}
